package com.atguigu.eduUcenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.eduUcenter.entity.UcenterMember;
import com.atguigu.eduUcenter.entity.vo.RegisterVo;
import com.atguigu.eduUcenter.mapper.UcenterMemberMapper;
import com.atguigu.eduUcenter.service.UcenterMemberService;
import com.atguigu.servicebase.Myexception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lhl
 * @since 2022-07-11
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    RedisTemplate<String,String>redisTemplate;
    @Override
    public String login(UcenterMember ucenterMember) {
        // 验证手机号和密码是否为空
        if(StringUtils.isEmpty(ucenterMember.getMobile())||StringUtils.isEmpty(ucenterMember.getPassword())){
            throw new GuliException(20001,"手机号或密码为空");
        }
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile",ucenterMember.getMobile());
        UcenterMember ucenterMember1 = baseMapper.selectOne(ucenterMemberQueryWrapper);
        if(ucenterMember1==null){
            throw new GuliException(20001,"没有该会员");
        }
        if(!MD5.encrypt(ucenterMember.getPassword()).equals(ucenterMember1.getPassword())){
            throw new GuliException(20001,"密码错误");
        }
        if(ucenterMember1.getIsDisabled()){
            throw new GuliException(20001,"该用户已被封");
        }
        String token = JwtUtils.getJwtToken(ucenterMember1.getId(), ucenterMember1.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 进行非空判断
        String code = registerVo.getCode();
        // 手机号
        String mobile = registerVo.getMobile();
        // 昵称
        String nickname = registerVo.getNickname();
        // 密码
        String password = registerVo.getPassword();
        if(StringUtils.isEmpty(code)|| StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname)|| StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败");
        }
        QueryWrapper<UcenterMember> regisWrapper = new QueryWrapper<>();
        regisWrapper.eq("mobile",mobile);
        if(baseMapper.selectCount(regisWrapper)>0){
            throw new GuliException(20001,"注册失败");
        }
        // 从redis中取出验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!redisCode.equals(code)){
            throw new GuliException(20001,"注册失败");
        }
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        // 将数据添加入数据库中
        this.save(member);
    }

    @Override
    public Integer countRegisterByDay(String day) {
        Integer integer = baseMapper.selectRegisterCount(day);
        return integer;
    }
}
